package com.cnswhy.cloud.server.merchant.controller.app;

import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.server.core.dto.SlideshowPageDTO;
import com.cnswhy.cloud.server.core.dto.SlideshowSaveDTO;
import com.cnswhy.cloud.server.core.dto.SlideshowUpdateDTO;
import com.cnswhy.cloud.server.core.entity.ShopBrand;
import com.cnswhy.cloud.server.core.entity.Slideshow;
import com.cnswhy.cloud.server.core.service.app.SlideshowService;
import com.cnswhy.cloud.server.merchant.util.OssUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 轮播图表
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/slideshow")
@Api(value = "Slideshow", tags = "轮播图表")
//@PreAuth(replace = "slideshow:")
public class SlideshowController extends SuperController<SlideshowService, Long, Slideshow, SlideshowPageDTO, SlideshowSaveDTO, SlideshowUpdateDTO> {
    @Autowired
    IdGeneratorService idGeneratorService;
    @PostMapping("/syncUrl")
    public R<Boolean> syncUrl() {
        List<Slideshow> shopBrands = baseService.list();
        for(Slideshow shopBrand : shopBrands){
            shopBrand.setUrl(OssUtils.updateLoadFileToOss("slideShow",  shopBrand.getUrl() ,idGeneratorService.genCommonIdString() + ".jpg"));
            baseService.updateById(shopBrand);
        }
        return success();
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Slideshow> slideshowList = list.stream().map((map) -> {
            Slideshow slideshow = Slideshow.builder().build();
            return slideshow;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(slideshowList));
    }
}
